<template>
  <div class="closing">
    <div>
      <jc-button
        :btn-arr="['reload','option', 'save', 'sub', 'pass', 'remove', 'list']"
        :disab-arr="[isDisabled && 'save', 'close', 'cancel', 'using', 'copy', 'freeze', 'com1', 'com2', 'toExcel']"
        :fbillno="subDetails.fbillNo"
        :list="'/adjustmentList'"
        @refresh="queryTBasBilltype"
        @save="newlyAdjustment"
        @submit="newlyAdjustment(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="del"
      />
    </div>
    <el-tabs @tab-click="handleOther">
      <el-tab-pane :label="$t('market.detailInfo')">
        <div class="header">
          <div class="header-name">
            <el-form :model="subDetails" label-width="100px" size="mini">
              <el-form-item :label="$t('market.state')">
                <el-tag :type="subDetails.stateColor" style="width: auto;">{{ subDetails.fdocumentstatus }}</el-tag>
              </el-form-item>
              <el-form-item :label="$t('market.billType')">
                <el-input v-model="subDetails.fbillTypeName" disabled />
              </el-form-item>
              <el-form-item :label="$t('market.businessType')">
                <el-input v-model="subDetails.fbussinesstypename" disabled />
              </el-form-item>
              <el-form-item :label="$t('market.period')">
                <el-input v-model.trim="subDetails.fdate" :disabled="isDisabled" />
              </el-form-item>
              <el-form-item :label="$t('market.billNo')">
                <el-input v-model.trim="subDetails.fbillNo" disabled />
              </el-form-item>
              <el-form-item :label="$t('market.note')">
                <el-input v-model="subDetails.fremark" :disabled="isDisabled" />
              </el-form-item>
            </el-form>
          </div>
        </div>
        <div class="table-content">
          <div style="margin-bottom: 5px;">
            <el-button size="mini" @click="newHang">{{ $t('btn.newHang') }}<!--新增行--></el-button>
            <el-button size="mini" @click="delHang">{{ $t('btn.delHang') }}<!--删除行--></el-button>
            <el-button size="mini" @click="batchFill">{{ $t('btn.batchFill') }}<!--批量填充--></el-button>
          </div>
          <jc-table
            v-loading="loading"
            :table-data="tableData"
            :table-header="tableHeader"
            :table-height="tableHeight"
            serial
            @cellClick="cellClick"
            @rowClick="rowClick"
          >
            <!--  物料编码-->
            <template v-slot:billSlot="col">
              <el-input
                v-model.trim="col.scope.row.materialNumber"
                size="mini"
                :disabled="isDisabled"
                @input="getAtuo(col.scope.$index, col.scope.row.materialNumber, 1)"
              >
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="materia(col.scope.$index)"
                />
              </el-input>
              <div v-if="aotuTab && material === col.scope.$index" :class="material<7 ? 'tabPosit': 'tabPosit1'">
                <number-code :fnumber="fnumber" @material="materialData" />
              </div>
            </template>
            <!--仓库-->
            <template v-slot:tagSlot="col">
              <el-input
                v-model="col.scope.row.stockName"
                size="mini"
                :disabled="isDisabled"
              >
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="handleWarehouse(col.scope.$index)"
                />
              </el-input>
            </template>
            <!--    调整金额    -->
            <template v-slot:btnState="col">
              <el-input-number v-model="col.scope.row.fadjustworth" size="mini" :disabled="isDisabled" />
            </template>
            <!--    备注  -->
            <template v-slot:btnStates="col">
              <el-input v-model.number="col.scope.row.fremark" size="mini" :disabled="isDisabled" />
            </template>
          </jc-table>
        </div>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="subDetails.fbillNo"
          other-height="68vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <!--物料弹窗-->
    <material v-if="isMateria" @material="materialData" />
    <!--仓库-->
    <selectCK v-if="seleceVisiblit" @client="selectData" />
  </div>
</template>

<script>
import {
  auditFinColas, delFinColas, notauditFinColas,
  notsubmitFinCola, queryFinColaDetails,
  submitFinCola, updateFinCola
} from '@/api/financial/warehousing'
import { toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'
import jcMarker from '@/components/marker'
import billFa from '@/views/financial/cashier/mixins'
import jumpMateriel from '@/components/JumpMateriel'
export default {
  name: 'AdjustmentDetails',
  components: {
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material'),
    selectCK: () => import('@/views/production/collect/components/seleceCK'),
    numberCode: () => import('@/components/NumberCode'),
    jcMarker,
    jcButton
  },
  mixins: [jumpMateriel, billFa],
  data() {
    return {
      isMateria: false,
      seleceVisiblit: false,
      aotuTab: false,
      material: 0,
      fnumber: '',
      subDetails: {},
      indexSelf: 0,
      loading: false,
      // 表头
      tableData: [], // 存货收发存明细表
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.fmaterial'), type: 'bill', prop: 'materialNumber' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('shop.fmodel'), prop: 'fmodel' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification' },
        { label: this.$t('shop.fdescripTion'), prop: 'fdescription', width: '200' },
        { label: this.$t('market.frombillNo'), prop: 'frombillNo' },
        { label: this.$t('market.billType'), prop: 'billtypename' },
        { label: this.$t('market.stock2'), type: 'tag', prop: 'stockName' },
        { label: this.$t('market.adjustAmount'), type: 'state', prop: 'fadjustworth' },
        { label: this.$t('market.entryRemark'), type: 'states', prop: 'fremark' }
      ],
      isDisabled: false,
      delArr: [],
      otherUrlObject: {}
    }
  },
  created() {
    if (this.$route.params.id) {
      this.queryTBasBilltype()
    }
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 260px)' : '73vh'
  },
  methods: {
    // 输入物料编码
    getAtuo(index, fnumber) {
      this.aotuTab = true
      this.material = index
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber = fnumber
      }, 600)
    },
    // 查询详情
    async queryTBasBilltype() {
      const DATA = { fid: this.$route.params.id }
      queryFinColaDetails(DATA).then(res => {
        if (res.code === 0) {
          this.delArr = []
          this.subDetails = toMxBmina(res.data)
          this.isDisabled = this.subDetails.fdocumentstatus === this.$t('market.state1') || this.subDetails.fdocumentstatus === this.$t('market.state2')
          this.otherUrlObject = res.data
          this.tableData = res.data.details
        }
      })
    },
    // 保存
    newlyAdjustment(ev) {
      const DATA = {
        ...this.subDetails,
        details: this.tableData,
        delArr: this.delArr
      }
      updateFinCola(DATA).then(res => {
        if (res.code === 0) {
          this.delArr = []
          ev === 1 ? this.submit(1) : this.queryTBasBilltype()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? submitFinCola : notsubmitFinCola
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? auditFinColas : notauditFinColas
      this.toExamineFun(api)
    },
    del() {
      this.$confirm('确定要删除该订单么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const DATA = { Numbers: [this.subDetails.fbillNo] }
        delFinColas(DATA).then(res => {
          if (res.code === 0) {
            this.$message({ showClose: true, message: res.message, type: 'success' })
            // 关闭当前页面
            this.$store.dispatch('tagsView/guanbi', this.$route)
            this.$router.push({ path: '/adjustmentList' })
          } else {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.subDetails.fbillNo] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
        this.queryTBasBilltype()
      })
    },
    handleOther(tab) {
      if (tab.name === 'log') {
        this.$refs.log.otherlog()
      }
    },
    // 打开物料
    materia(index) {
      this.indexSelf = index
      this.isMateria = true
    },
    materialData(ev) {
      if (ev.fnumber) {
        const item = this.tableData[this.indexSelf]
        item.fmodel = ev.fmodel
        item.materialNumber = ev.fnumber
        item.fname = ev.fname
        item.fmaterialId = ev.fmaterialId
        item.fspecification = ev.fspecificaTion
        item.fdescription = ev.fdescripTion
        item.funitName = ev.funit
        item.stockName = ev.fstock
        item.fstockId = ev.fstockId
        this.aotuTab = false
      }
      this.isMateria = false
    },
    // 仓库
    handleWarehouse(index) {
      this.indexSelf = index
      this.seleceVisiblit = true
    },
    selectData(ev) {
      if (ev.fstockId) {
        const item = this.tableData[this.indexSelf]
        item.fstockId = ev.fstockId
        item.stockName = ev.fname
      }
      this.seleceVisiblit = false
    },
    newHang() {
      this.tableData.push({
        fentryId: 0,
        billtype: 0,
        billentryId: 0,
        materialNumber: '',
        frombillNo: '',
        billtypename: '',
        stockName: '',
        fremark: '',
        fadjustworth: ''
      })
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 删除行
    delHang() {
      const fentryId = this.tableData[this.indexSelf].fentryId
      this.delArr.push(fentryId)
      this.tableData.splice(this.indexSelf, 1)
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;
  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      height: 35px;
      width: 268px;
      margin-bottom: 5px;
      //&::v-deep .el-input--mini .el-input__inner{
      //  height: 22px;
      //}
      .btnType {
        margin-right: 20px;
      }

      .el-date-editor.el-input {
        width: 100% !important;
      }

      .el-select {
        width: 100%;
      }
    }
  }
}
</style>
